﻿using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Windows.Forms;


namespace BookMS
{
    public partial class login : Form
    {
        Model.user m_uinfo = new Model.user();
        Model.admin m_ainfo = new Model.admin();
        BLL.Base b_login = new BLL.Base();

        public login()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {
            //编写数据库连接串
            string connStr = "server=localhost;uid=root;pwd=root;database=bookdb";
            //创建SqlConnection的实例
            MySqlConnection conn = null;
            try
            {
                conn = new MySqlConnection(connStr);
                //打开数据库连接
                conn.Open();
                MessageBox.Show("数据库连接成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据库连接失败！" + ex.Message);
            }
            finally
            {
                if (conn != null)
                {
                    //关闭数据库连接
                    conn.Close();
                }
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBoxname.Text != "" && textBoxpwd.Text != "")
            {
                Login();
            }
            else
            {
                MessageBox.Show("输入有空项，请重新输入");//弹窗提示
            }
        }
        //登录验证，验证是否允许登录，允许返回真
        public void Login()
        {
            //用户
            if (radioButtonuser.Checked == true)
            {
                m_uinfo.id = textBoxname.Text.Trim().ToString();
                m_uinfo.pwd = textBoxpwd.Text.Trim().ToString();
                if (b_login.userlogin(m_uinfo)>0)
                {
                    Data.UID = m_uinfo.id.ToString();
                    Data.UName = m_uinfo.pwd.ToString();
                    MessageBox.Show("登录成功");
                    user u = new user();
                    this.Hide();//隐藏旧窗体
                    u.ShowDialog();//以对话框形式显示
                    this.Show();//显示新窗体
                }
                else
                {
                    MessageBox.Show("登录失败");

                }
                //dataBase.DataBaseClose();

                //MessageBox.Show(dc[0].ToString() + dc["name"].ToString());
            }
            //管理员
            if (radioButtonadmin.Checked == true)
            {
                //DataBase dataBase = new DataBase();
                //string sql = $"SELECT * FROM `admin` WHERE id = '{textBoxname.Text }' AND pwd = '{textBoxpwd.Text}'";
                //IDataReader dc = dataBase.Read(sql);
                m_ainfo.id = textBoxname.Text.Trim().ToString();
                m_ainfo.pwd = textBoxpwd.Text.Trim().ToString();
                if (b_login.userlogin(m_ainfo)>0)
                {
                    MessageBox.Show("登录成功");
                    admin a = new admin();
                    this.Hide();
                    a.ShowDialog();
                    this.Show();
                }
                else
                {
                    MessageBox.Show("登录失败");

                }
                //dataBase.DataBaseClose();
            }

        }

        private void textBoxpwd_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
